using Models.Models;
using Microsoft.EntityFrameworkCore;

namespace Services;

/// <summary>
/// 首页服务实现
/// </summary>
public class UserService : IUserService
{
  private readonly ShopxoContext _context;

  public UserService(ShopxoContext context)
  {
    _context = context;
  }

  /// <summary>
  /// 获取首页导航数据
  /// </summary>
  public async Task<IEnumerable<object>> GetUserById(int id)
  {
    return await _context.SUser
        .Where(x => x.Id == id)
        .Select(
          x => new
          {
            id = x.Id,
            username = x.Username,
            nickname = x.Nickname,
            avatar = x.Avatar,
            mobile = x.Mobile,
            email = x.Email
          }
        )
        .ToListAsync();
  }

  public async Task<IEnumerable<object>> GetUserByMobile(string mobile)
  {
    return await _context.SUser
        .Where(x => x.Mobile == mobile)
        .Select(
          x => new
          {
            id = x.Id,
            username = x.Username,
            nickname = x.Nickname,
            avatar = x.Avatar,
            mobile = x.Mobile,
          }
        )
        .ToListAsync();
  }
}